package com.shanghaionstar.user;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConfigParams;

import java.util.Locale;

/**
 * Created by Mengzhou Gu on 2016/8/2.
 */
@ContentView(R.layout.fragment_container)
public class PrePayActivity extends SwipeActivity {
    public FragmentManager fragmentManager;
//    public String current_fragment = null;
    @FindViewById(value = R.id.fragment_container, background = R.drawable.onstar_join_me_bg)
    private LinearLayout fragment_container;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        PayCardFragment payCardFragment = new PayCardFragment();
        fragmentManager = getSupportFragmentManager();
        changeFragment(fragmentManager, payCardFragment, PayCardFragment.class, false);
        //changeFragment(fragmentManager,new PayCardDetailFragment(),PayCardDetailFragment.class,false);
        // changeFragment(fragmentManager,new PayCardVisitorActiveFragment(),PayCardVisitorActiveFragment.class,false);
        initPPCFragment();
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    private void initPPCFragment() {
        // 如果正在登陆不能点击 弹出正在连接车的提示
        String login_role = ApplicationUtil.getInstance().getLoginRole();
        if (ApplicationUtil.getInstance().isLogin()) {
            if (!CommonUtil.isNull(login_role) && login_role.toLowerCase(Locale.CHINA).equals(Constants.OWNER)
                    || login_role.toLowerCase(Locale.CHINA).equals(Constants.PROXY)
                    || login_role.toLowerCase(Locale.CHINA).equals(Constants.DRIVER)) {
                // 车主
                changeFragment(fragmentManager, new PayCardFragment(), PayCardFragment.class, true);
            } else if (!CommonUtil.isNull(login_role) && login_role.toLowerCase(Locale.CHINA).equals(Constants.VISITOR)) {
                // 潜客
                changeFragment(fragmentManager, new PayCardOtherActiveFragment(), PayCardOtherActiveFragment.class, false);
            }

        } else {
            // 没有自动登陆
            changeFragment(fragmentManager, new PayCardVisitorActiveFragment(), PayCardVisitorActiveFragment.class, false);
        }
    }

//    public void setCurrentFragment(String fragment) {
//        this.current_fragment = fragment;
//    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (getCurrentFragmentTag().equals("ActiviteHistoryFragment") && PreferencesUtls.getInstance(this).getBoolean(FinalConfigParams.OWNER_JUMP, true)) {
                finish();
            }
//            else if (current_fragment.equals("PayCardOtherActiveFragment")) {
//                BusinessUtil.sendReportByClickType(this, FunctionNameFindId.Activeforothers_back.getStringValue());
//            }
            else if (getCurrentFragmentTag().equals("PayCardVisitorActiveFragment")) {
                finish();
            } else if (getCurrentFragmentTag().equals("PayCardActiveSuccessFragment")) {
                finish();
            }
//            else if (current_fragment.equals("PayCardFragment")) {
//                finish();
//            }
            else {
                getFragment(getCurrentFragmentTag()).onFragmentBackPress();
                return true;
            }
        }

        return false;
    }

//    @Override
//    public void onBackPressed() {
//        super.onBackPressed();
//        if (current_fragment.equals("ActiviteHistoryFragment") && PreferencesUtls.getInstance(this).getBoolean(FinalConfigParams.OWNER_JUMP, true)) {
//            finish();
//        } else if (current_fragment.equals("PayCardVisitorActiveFragment")) {
//            finish();
//        } else if (current_fragment.equals("PayCardActiveSuccessFragment")) {
//            finish();
//        } else {
//            getFragment(current_fragment).onFragmentBackPress();
//        }
//    }

    public void hideSoftInput() {
        if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
            ((InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }

}
